package com.four.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.four.shop.config.TokenConfig;
import com.four.shop.entity.Manage;
import com.four.shop.service.ManageService;
import com.four.shop.vo.ManageVo;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:wsy
 * @Date: 2021/12/9/15:35
 */
@RestController
@RequestMapping("shop/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 根据编号查询商品信息
     *
     * @param id ：编号
     * @return ：返回查询到的数据
     */
    @GetMapping("/getShopById/{id}")
    public R getShopById(@PathVariable(value = "id") Integer id) {
        Manage list = manageService.getShopById(id);
        return R.ok().put("list", list);
    }

    /**
     * 商品管理条件查询
     *
     * @param map ：分页一类参数
     * @return ：返回列表
     */
    @GetMapping("/managePageList")
    public R managePageList(@RequestParam Map<String, Object> map) {
        PageUtils page = manageService.managePageList(map);
        return R.ok().put("page", page);
    }

    /**
     * 商品管理数据删除
     *
     * @param manId ：编号
     * @return ：返回状态
     */
    @DeleteMapping("manageDeleteById/{manId}")
    public R manageDeleteById(@PathVariable("manId") Integer manId) {

        return manageService.manageDeleteById(manId);
    }

    /**
     * 商品添加
     *
     * @param manage
     * @return
     */
    @PostMapping("/save")
    public R addShop(@RequestBody Manage manage) {
        manageService.addShop(manage);
        return R.ok();
    }

    /**
     * 根据id上传es
     *
     * @param id
     * @return
     */
    @PutMapping("manUp/{id}")
    public R mangeUp(@PathVariable("id") Integer id) {
        manageService.manUp(id);
        return R.ok();
    }

    /**
     * 下架
     *
     * @param id
     * @return
     */

    @PutMapping(value = "manDown/{id}")
    public R manDown(@PathVariable("id") Integer id) {
        manageService.manDown(id);
        return R.ok();
    }

    /**
     * 加入回收站
     *
     * @param id
     * @return
     */

    @PutMapping("updateState/{id}")
    public R updateState(@PathVariable(value = "id") Integer id) {
        Manage manage = manageService.selectMage(id);
        manage.setState(4);
        manage.setUpdateTime(new Date());
        manageService.updateState(manage);
        return R.ok();
    }

    /**
     * 回复
     *
     * @param id
     * @return
     */
    @PutMapping("updateState2/{id}")
    public R updateState2(@PathVariable(value = "id") Integer id) {
        Manage manage = manageService.selectMage(id);
        manage.setState(1);
        manage.setUpdateTime(new Date());
        manageService.updateState(manage);
        return R.ok();
    }

    /**
     * 多表添加
     *
     * @param manageVo ：进行多表添加接收的参数
     * @return ：返回成功与否
     */
    @PostMapping("/saveManage")
    public R saveManage(@RequestBody ManageVo manageVo, HttpServletRequest request) {
        String token = request.getHeader("token");

        return manageService.saveManage(manageVo,token);
    }

    /**
     * 生成token
     * @return 返回token值
     */
    @GetMapping("/getToken")
    public R getToken() {
        String save = TokenConfig.generateToken("save");
        return R.ok().put("data",save);
    }

    /**
     *fegin调用所使用的查所有商品
     * @return
     */
    @GetMapping("/findAllManage")
    public List<Manage> findAllManage(){
        return manageService.findAllManage();
    }
    /**
     * fegin调用所使用的根据id所有商品
     * @param id
     * @return
     */
    @GetMapping("/getManageById/{id}")
    public Manage getManageById(@PathVariable(value = "id") Integer id) {
        Manage manage = manageService.getManageById(id);
        return manage;
    }

    @PostMapping("/ManageList")
    public R bargainList(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize,
            @RequestBody ManageVo vo
    ){
        IPage<Manage> list = manageService.getfindAll(page,pageSize,vo);
        return R.ok().put("list",list);
    }

}
